package com.iafme.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.jwt.JWTUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.operator.InList;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iafme.configuration.PayConfiguration;
import com.iafme.dto.req.*;
import com.iafme.dto.res.*;
import com.iafme.model.Certificate;
import com.iafme.properties.ScanPayReq;
import com.iafme.model.Level;
import com.iafme.model.Project;
import com.iafme.model.Showings;
import com.iafme.service.*;
import com.iafme.utils.ExamSignUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/4 17:10
 * 考试中心
 */
@RestController
@RequestMapping(value = "/exam")
@AllArgsConstructor
public class ExamController {

    private final IProjectService projectService;

    /**
     * 报考项目
     * @return
     */
    @GetMapping(value = "/projectLists")
    public List<ProjectResDTO> projectLists(){
        List<Project> list = projectService.list();
        return Convert.toList(ProjectResDTO.class, list);
    }

    private final ILevelService levelService;

    /**
     * 报考等级
      * @param projectId
     * @return
     */
    @GetMapping(value = "/levelInfo/{projectId}")
    public List<EnterLevelResDTO> enterLevelList(@PathVariable Integer projectId){
        LambdaQueryWrapper<Level> eq = Wrappers.lambdaQuery(Level.class).eq(Level::getProjectId, projectId);
        return Convert.toList(EnterLevelResDTO.class, levelService.list(eq));
    }

    private final IShowingsService showingsService;

    /**
     * 报考场次
     * @param levelId
     * @return
     */
    @GetMapping(value = "/showingsLists/{projectId}/{levelId}")
    public List<ShowingsResDTO> showingsList(@PathVariable Integer projectId, @PathVariable Integer levelId){
        LambdaQueryWrapper<Showings> eq = Wrappers.lambdaQuery(Showings.class).eq(Showings::getLevelId, levelId).eq(Showings::getProjectId, projectId);
        return Convert.toList(ShowingsResDTO.class, showingsService.list(eq));
    }

    private final CertificateService certificateService;

    /**
     * 申请证书
     * @param projectId
     * @return
     */
    @GetMapping(value = "/certificateList/{projectId}")
    public List<CertificateResDTO> certificateList(@PathVariable Integer projectId){
        LambdaQueryWrapper<Certificate> eq = Wrappers.lambdaQuery(Certificate.class).eq(Certificate::getProjectId, projectId);
        return Convert.toList(CertificateResDTO.class,certificateService.list(eq));
    }


    /**
     * 生成报名信息
     * @return
     */
    @PostMapping(value = "/createSign")
    public String createSign(@RequestBody ExamReqDTO examReqDTO){
        return ExamSignUtils.createSign(examReqDTO);
    }

    private final ISubjectService subjectService;

    /**
     * 验证签名
     * @param sign
     * @return
     */
    @PostMapping(value = "/sign")
    public List<SubjectResDTO> parseSign(String sign){

        return subjectService.parseSign(sign);
    }





    private final PayConfiguration payConfiguration;

    /**
     * 生成支付二维码
     * @param orderPayReqDTO
     * @return
     */
    @PostMapping(value = "/sendPayCode")
    public ScanPayReq sendPayCode(@RequestBody OrderPayReqDTO orderPayReqDTO, HttpServletRequest request){

        return payConfiguration.payCode(orderPayReqDTO,request);
    }

    /**
     * 证书申请扫码支付
     * @param certificateOrderReqDTO
     * @param request
     * @return
     */
    @PostMapping(value = "/certificatePayCode")
    public ScanPayReq certificatePayCode(@RequestBody CertificateOrderReqDTO certificateOrderReqDTO,HttpServletRequest request){

        return payConfiguration.certificatePayCode(certificateOrderReqDTO,request);
    }



    private final BeanSearcher beanSearcher;

    //考试模块
    @GetMapping(value = "/courseLists/{showingsId}")
    public List<CourserResDTO> courseLists(@PathVariable Integer showingsId){
        Map<String, Object> map = MapUtils.builder().field(CourserResDTO::getShowingsId, showingsId).op(Equal.class).build();
        return beanSearcher.searchList(CourserResDTO.class,map);
    }

    @PostMapping(value = "/courseLists")
    public List<CourserResDTO> courseLists(@RequestBody CourseParamReqDTO courseParamReqDTO){
        Map<String, Object> map = MapUtils.builder().field(CourserResDTO::getId, courseParamReqDTO.getIds()).op(InList.class).build();
        return beanSearcher.searchList(CourserResDTO.class,map);
    }

    /**
     * 证书
     * @param certificateReqDTO
     * @return
     */
    @PostMapping(value = "/certificateLists")
    public List<CertificateResDTO> certificateLists(@RequestBody CertificateReqDTO certificateReqDTO){
        Map<String, Object> map = MapUtils.builder().field(CertificateResDTO::getId, certificateReqDTO.getCertificateIds()).op(InList.class).build();
        return beanSearcher.searchList(CertificateResDTO.class,map);
    }


    /**
     * 解析zip rar文件
     */
    private final  ZipFileService zipFileService;

    @PostMapping(value = "/uploadZip")
    public List<FileResDTO> uploadZip(MultipartFile file){

        return zipFileService.zip(file);
    }

}
